if 0:
	import xmlrpcTransport as Transport
	tadr="http://localhost:7557"
else:
	import filesystemTransport as Transport
	tadr="/svc/local/awx-register" # TODO !!
	
#~ from etoffiutils2.list import head, tail, cons
import time, sys, os
import Queue
#

#
mq = Queue.Queue()
mqRegister='mqRegister'
#

targets={}
running=None

############################################################################
############################################################################

import threading 

def default_exeecutor1(target,args):
	Running = threading.Thread(target=thr, args=[targets])
	Running.setDaemon(1)
	return Running

def default_exeecutor(target,args):
	Running = None
	apply(target, args)
	#Running.setDaemon(1)
	return 'ok' #Running

############################################################################	
############################################################################

def initializeMsgTarget(name, targ, ex=default_exeecutor):
	global running
	if not running:
		running = ex(thr,[targets])
		if hasattr(running, 'start'):
			running.start()
	#
	global targets
	assert name not in targets, "Target already defined"
	nt=MessageTarget()
	nt.target=targ
	nt.targname=name
	nt.initializeMsgTarget()
	targets[name]=nt
	#
	return nt

def thr(targs):
	global mq, connclient, connid
	quit=0
	#
	
	## Create Transport
	T = Transport.xx()
	#~ T.verbose = 2
	
	## start xmlrpc client
	connclient=Transport.T(tadr, transport=T)
	connid = connclient.gett("getConnId")
	
	##
	while not quit:
		time.sleep(.125)
		print ".",
		try:
			## pull an op ...
			op = mq.get_nowait()
			## execute op
			if op[0] is mqRegister:
				_, mto, tn = op
				ti=connclient.registerClient(tn)
				print 7,ti
				mto.targetInfo=ti[2]
			elif op[0] is mqQuit:
				quit=1
			else:
				raise 'fuckyou'
		except Queue.Empty:
			try:
				msg=connclient.gett("requestAnyMessage")(connid)
				targets[msg.targ].queue.put(msg)
			except:
				pass

class Message:
	def __init__ (self, src, targ, main):
		self.src = src
		self.targ = targ
		self.main = main
		self.payload = {}
	def add (self, n, v):
		self.payload[n]=v
	def send (self):
		print 76, self.text(), self.src, self.src.targetInfo
		self.src._connclient.queueMessage_From (self.text(), self.src.targetInfo)
	def __str__(self):
		return `self.text()`
	def text(self):
		return [self.main, self.targ, self.payload]
	def __getitem__(self, a):
		return self.payload[a]

#~ class MessageTargetCommThread(Thread):
	#~ def run(self):
		#~ apply (self._targ._initializeMsgTarget, self._args)

class MessageTarget:
	def targetFor (self, target):
		T = self._connclient.targetFor (target)
		TGT = T
		return TGT
	def initializeMsgTarget(self):
		#
		self.queue = Queue.Queue()
		self._initializeMsgTarget(self.targname)
		#
	def _initializeMsgTarget(self, name):
		#~ FGH = 21345
		#
		global mq
		mq.put([mqRegister, self, name])
		#
	def notifyMessage(self, msg):
		# block reciever until consumer eats head
		self.queue.put(msg)
	#~ def GetMessage(self):
		#~ # block for a message 
		#~ # runs in its own thread
		#~ R=None
		#~ try:
			#~ R=self.queue.get()
		#~ except Queue.Empty:
			#~ assert 0 # not reached because we are blocking
			#~ pass
		#~ assert R!=None
		#~ # self.target.handle(R)
		#~ return R

#
# eof
#
